package org.jetnuke.dao.sqlserver;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

public class SqlServerLinkLinkDAO extends
		org.jetnuke.dao.generic.GenericLinkLinkDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(SqlServerLinkLinkDAO.class);

	public List search(String title, String description, int offset,
			int rowcount) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("search(String, String, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.search");
		q = q.replaceAll("#start", String.valueOf(offset));
		q = q.replaceAll("#limit", String.valueOf(rowcount));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setString(1, title);
		p.setString(2, description);
		p.setString(3, title);
		p.setString(4, description);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("search(String, String, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectDateAsc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectDateAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectDateAsc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectDateAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectDateDesc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectDateDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectDateDesc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectDateDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectHitsAsc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectHitsAsc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectHitsDesc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectHitsDesc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectHitsDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectRatingAsc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectRatingAsc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectRatingDesc(int cid, int start, int limit)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectRatingDesc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectRatingDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectTitleAsc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleAsc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectTitleAsc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleAsc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectTitleDesc(int cid, int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleDesc(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectTitleDesc");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		p.setInt(1, cid);
		p.setInt(2, cid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectTitleDesc(int, int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectTopDown(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectTopDown(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("LinkLinkModel.selectTopDown");
		p = JForumExecutionContext.getConnection().prepareStatement(
				q1 + " " + limit + " " + q2);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectTopDown(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List getByHitsDown(int start, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getByHitsDown(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;
		String q = SystemGlobals.getSql("LinkLinkModel.selectByHitsDown");
		q = q.replaceAll("#start", String.valueOf(start));
		q = q.replaceAll("#limit", String.valueOf(limit));
		p = JForumExecutionContext.getConnection().prepareStatement(q);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getByHitsDown(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectWithRating(int totalVotesMin, int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectWithRating(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("LinkLinkModel.selectWithRating");
		p = JForumExecutionContext.getConnection().prepareStatement(
				q1 + " " + limit + " " + q2);
		p.setInt(1, totalVotesMin);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectWithRating(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}
}
